package daoIpmt;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import beans.Typer;
import dao.TypeDao;
import utils.DataSourceUtils;

public class TypeDaoIpmt implements TypeDao {
	QueryRunner qu = new QueryRunner(DataSourceUtils.getDataSource());
	@Override
	public void addType(Typer typer) {
		String sql= "insert into type values(?,?)";
		try {
			
			qu.update(DataSourceUtils.getConnection(),sql,typer.getUsername(),typer.getType());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Override
	public List<Typer> findUsernameByType() {
		String sql = "select * from type where type = ?";
		try {
			List<Typer> list =qu.query(sql, new BeanListHandler<Typer>(Typer.class));
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Typer findTypeByUsername(String username) {
		String sql ="select * from type where username = ?";
		try {
			Typer type = qu.query(sql, new BeanHandler<Typer>(Typer.class),username);
			return type;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public void modType(Typer typer) {
		String sql= "update type set type = ? where username = ?";
		try {
			
			qu.update(DataSourceUtils.getConnection(),sql,typer.getType(),typer.getUsername());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
